package com.jxw.utils;

import android.util.Log;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/* loaded from: classes.dex */
public class SHAUtil {
    public static final String TAG = SHAUtil.class.getSimpleName();

    public static String getTextSHA256(String str) {
        return SHA(str, "SHA-256");
    }

    public static String getTextSHA512(String str) {
        return SHA(str, "SHA-512");
    }

    private static String SHA(String str, String str2) {
        if (str != null && str.length() > 0) {
            try {
                MessageDigest messageDigest = MessageDigest.getInstance(str2);
                messageDigest.update(str.getBytes());
                byte[] digest = messageDigest.digest();
                StringBuffer stringBuffer = new StringBuffer();
                for (byte b : digest) {
                    String hexString = Integer.toHexString(b & 255);
                    if (hexString.length() == 1) {
                        stringBuffer.append('0');
                    }
                    stringBuffer.append(hexString);
                }
                return stringBuffer.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static byte[] createChecksum(String str, String str2) throws Exception {
        int read;
        FileInputStream fileInputStream = new FileInputStream(str);
        byte[] bArr = new byte[1024];
        MessageDigest messageDigest = MessageDigest.getInstance(str2);
        do {
            read = fileInputStream.read(bArr);
            if (read > 0) {
                messageDigest.update(bArr, 0, read);
            }
        } while (read != -1);
        fileInputStream.close();
        return messageDigest.digest();
    }

    public static String getMD5Checksum(String str) throws Exception {
        return byte2String(createChecksum(str, "MD5"));
    }

    public static String getSHA256Checksum(String str) throws Exception {
        return byte2String(createChecksum(str, "SHA-256"));
    }

    public static String getSHA512Checksum(String str) throws Exception {
        return byte2String(createChecksum(str, "SHA-512"));
    }

    private static String byte2String(byte[] bArr) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bArr) {
            sb.append(Integer.toString((b & 255) + 256, 16).substring(1));
        }
        Log.i(TAG, "checkSum: " + sb.toString());
        return sb.toString();
    }
}
